package servlet;

import com.atguigu.login.dao.UserDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        //获取数据
        System.out.println("post 请求");
        // 封装User
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");

        System.out.println(userName + "----" + password);

        //获取 Dao
        UserDaoImpl userdao = new UserDaoImpl();

        //直接登录
        if(userdao.getUser(userName,password) == null){
            System.out.println("登录失败");
            response.sendRedirect("login.jsp");
        }else{
            System.out.println("登陆成功");
            response.sendRedirect("index.jsp");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(">>>>进来了");
    }
}
